import javax.swing.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //这个二叉树的根节点
    //public TreeNode root;
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //求结点个数
    //左右相加
    public int size1(TreeNode root){
        if(root == null){
            return 0;
        }
        return size1(root.left)+size1(root.right) + 1;
    }
    //遍历思路
    public static int  sz = 0;
    public void size2(TreeNode root){
        if(root == null){
            return;
        }
        sz++;
        size2(root.left);
        size2(root.right);
    }
    //求叶子个数
    public int getLeafNodeCount1(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount1(root.left) + getLeafNodeCount1(root.right);
    }
    //遍历思路
    public int leafSize = 0;
    void getLeafNodeCount2(TreeNode root){
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }
    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root, int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root, k - 1);
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return (left > right ? left + 1 : right + 1);
    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode ret1 = find(root.left, val);
        if(ret1 != null){
            return ret1;
        }
        TreeNode ret2 = find(root.right, val);
        if(ret2 != null){
            return ret2;
        }
        return null;
    }
}

